#ifndef CONFIGDIALOG_H
#define CONFIGDIALOG_H

#include <QDialog>
#include <QLineEdit>
#include <QSpinBox>
#include <QPushButton>
#include <QComboBox>
#include <QCheckBox>
#include <QTabWidget>
#include <QTableWidget>
#include <QGroupBox>
#include <QRadioButton>
#include <QLabel>
#include <QTextEdit>
#include <QSlider>
#include <QFontComboBox>
#include <QVBoxLayout>
#include <QHBoxLayout>
#include "connectionmanager.h"

class ConfigDialog : public QDialog
{
    Q_OBJECT

public:
    explicit ConfigDialog(QWidget *parent = nullptr);
    ~ConfigDialog();

    // 配置管理
    void setProfile(const ConnectionManager::ConnectionProfile &profile);
    ConnectionManager::ConnectionProfile getProfile() const;
    void setMode(const QString &mode); // "create", "edit", "quick"

    // 验证
    bool validateInput() const;
    void createUI();
signals:
    void profileCreated(const ConnectionManager::ConnectionProfile &profile);
    void profileUpdated(const ConnectionManager::ConnectionProfile &profile);
    void testConnectionRequested(const ConnectionManager::ConnectionProfile &profile);

private slots:
    void browsePrivateKey();
    void testConnection();
    void saveProfile();
    void resetToDefaults();
    void onAuthMethodChanged();
    void onAdvancedSettingsChanged();
    void updatePasswordStrength();
    void generateKeyPair();
    void checkConnection();
    void onColorButtonClicked();

private:
    // UI组件 - 基本设置
    QLineEdit *m_nameEdit;
    QLineEdit *m_hostEdit;
    QSpinBox *m_portSpin;
    QLineEdit *m_usernameEdit;
    QComboBox *m_authMethodCombo;
    QLineEdit *m_passwordEdit;
    QLineEdit *m_privateKeyEdit;
    QLineEdit *m_passphraseEdit;
    QPushButton *m_browseKeyButton;
    QPushButton *m_generateKeyButton;
    QTextEdit *m_descriptionEdit;
    QComboBox *m_groupCombo;
    QCheckBox *m_favoriteCheck;

    // UI组件 - 高级设置
    QSpinBox *m_timeoutSpin;
    QCheckBox *m_keepAliveCheck;
    QSpinBox *m_keepAliveIntervalSpin;
    QCheckBox *m_compressionCheck;
    QComboBox *m_terminalTypeCombo;
    QSpinBox *m_portForwardLocalSpin;
    QLineEdit *m_portForwardRemoteHostEdit;
    QSpinBox *m_portForwardRemoteSpin;

    // UI组件 - 终端设置
    QFontComboBox *m_fontCombo;
    QSpinBox *m_fontSizeSpin;
    QSlider *m_transparencySlider;
    QLabel *m_transparencyValueLabel;
    QCheckBox *m_bellCheck;
    QCheckBox *m_cursorBlinkCheck;
    QComboBox *m_cursorShapeCombo;
    QSpinBox *m_scrollbackSpin;
    QCheckBox *m_wordWrapCheck;
    QCheckBox *m_altScreenCheck;

    // UI组件 - 外观设置
    QPushButton *m_fgColorButton;
    QPushButton *m_bgColorButton;
    QPushButton *m_cursorColorButton;
    QPushButton *m_selectionColorButton;
    QPushButton *m_blackColorButton;
    QPushButton *m_redColorButton;
    QPushButton *m_greenColorButton;
    QPushButton *m_yellowColorButton;
    QPushButton *m_blueColorButton;
    QPushButton *m_magentaColorButton;
    QPushButton *m_cyanColorButton;
    QPushButton *m_whiteColorButton;
    QPushButton *m_brightBlackColorButton;
    QPushButton *m_brightRedColorButton;
    QPushButton *m_brightGreenColorButton;
    QPushButton *m_brightYellowColorButton;
    QPushButton *m_brightBlueColorButton;
    QPushButton *m_brightMagentaColorButton;
    QPushButton *m_brightCyanColorButton;
    QPushButton *m_brightWhiteColorButton;

    // UI组件 - 按钮
    QPushButton *m_testButton;
    QPushButton *m_saveButton;
    QPushButton *m_cancelButton;
    QPushButton *m_resetButton;

    // 状态
    QString m_mode;
    QString m_currentProfileId;
    QTabWidget *m_tabWidget;

    // 创建UI
    void createBasicSettingsTab();
    void createAdvancedSettingsTab();
    void createTerminalSettingsTab();
    void createAppearanceSettingsTab();
    void createButtons();

    // 布局
    QVBoxLayout *createBasicSettingsLayout();
    QVBoxLayout *createAdvancedSettingsLayout();
    QVBoxLayout *createTerminalSettingsLayout();
    QVBoxLayout *createAppearanceSettingsLayout();
    QHBoxLayout *createButtonsLayout();

    // 辅助方法
    void loadSettings();
    void saveSettings();
    void resetSettings();
    void updateGroupCombo();
    void updateAuthMethodUI();
    void setColorButtonColor(QPushButton *button, const QColor &color);
    QColor getColorFromButton(QPushButton *button) const;

    // 验证
    bool validateBasicSettings() const;
    bool validateAdvancedSettings() const;
    bool validateTerminalSettings() const;
    bool validateAppearanceSettings() const;

    // 测试连接
    bool testConnectionInternal(const ConnectionManager::ConnectionProfile &profile);
    void showTestResult(bool success, const QString &message = QString());

    // 加密
    QString encryptPassword(const QString &password) const;
    QString decryptPassword(const QString &encrypted) const;

    // 错误处理
    void showError(const QString &title, const QString &message);
    void showInfo(const QString &title, const QString &message);
    void showWarning(const QString &title, const QString &message);
};

#endif // CONFIGDIALOG_H
